package com.hs.srm.admin.modules.pricemanagement.controller;

import com.hs.srm.admin.modules.inforecord.entity.InforecordEntity;
import com.hs.srm.admin.modules.inforecord.entity.InforecordLogEntity;
import com.hs.srm.admin.modules.inforecord.service.impl.InforecordLogService;
import com.hs.srm.admin.modules.pricemanagement.entity.EssentialInformationEntity;
import com.hs.srm.admin.modules.pricemanagement.service.EssentialInformationService;
import com.hs.srm.common.utils.DozerUtil;
import com.sap.conn.jco.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

/**
 * @Description:
 * @Author socik
 * @Date 2019/5/5 15:41
 */
@Component("essentialInformationTask")
public class EssentialInformationTask {
    Logger logger = LoggerFactory.getLogger(EssentialInformationTask.class);
    @Autowired
    private EssentialInformationService essentialInformationServiceimpl;
    @Autowired
    private InforecordLogService inforecordLogService;

    /**
     * @Description runEinfoTask
     * @Param []
     * @Return void
     * @Author socik
     * @Date 2019/5/5 15:39
     */
    public void runEinfoTask() {
        logger.debug("信息记录同步开始!");
        EssentialInformationEntity essentialInformationEntity = new EssentialInformationEntity();
        InforecordLogEntity inforecordLogEntity = new InforecordLogEntity();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
        JCoDestination destination = null;
        JCoFunction function = null;
        JCoParameterList list = null;
        JCoParameterList tables = null;
        JCoParameterList returnlist = null;
        try {
            destination = JCoDestinationManager.getDestination("IDOC_AFS");
            function = destination.getRepository().getFunction("ZFM_SRM_INFO");
            list = function.getImportParameterList();
            tables = function.getTableParameterList();
            returnlist = function.getExportParameterList();

        } catch (JCoException e) {
            e.printStackTrace();
        }
        list.setValue("IV_EKORG", "1000");
        try {
            function.execute(destination);
        } catch (JCoException e) {
            e.printStackTrace();
        }
        JCoTable returnTable = tables.getTable("ET_TAB");
        //SAP返回数据
        JCoStructure returnInfo = returnlist.getStructure("ES_RETURN");
        for (int x = 0; x < returnTable.getNumRows(); x++) {
            //成品的物料是3开头的，OEM的申请单同步到srm处理，现在需要3 开头的物料
        /* if("3".equals(((String)returnTable.getValue("MATNR")).substring(0,1))){
             continue;
         }*/
            returnTable.setRow(x);
            Object object = returnTable.getValue("NETPR");
            //价格为0 不保存信息记录
            if (object.toString().equals("0")) {
                continue;
            }
            essentialInformationEntity.setPrice(new BigDecimal(object.toString()));
            essentialInformationEntity.setInfoCode((String) returnTable.getValue("INFNR"));
            essentialInformationEntity.setWerks((String) returnTable.getValue("WERKS"));
            essentialInformationEntity.setMaterialCode((String) returnTable.getValue("MATNR"));
            essentialInformationEntity.setMaterialGroup((String) returnTable.getValue("MATKL"));
            essentialInformationEntity.setSrmCode((String) returnTable.getValue("LIFNR"));
            essentialInformationEntity.setPurchaseGroup((String) returnTable.getValue("EKGRP"));
            essentialInformationEntity.setPurchArg((String) returnTable.getValue("EKORG"));
            essentialInformationEntity.setTaxCode((String) returnTable.getValue("MWSKZ"));
            essentialInformationEntity.setCurrency((String) returnTable.getValue("WAERS"));
            essentialInformationEntity.setPriceUnit(returnTable.getValue("PEINH").toString());
            essentialInformationEntity.setI18nTradeTerms(returnTable.getValue("INCO1") + (String) returnTable.getValue("INCO2"));
            essentialInformationEntity.setStartData(((Date) returnTable.getValue("DATAB")));
            essentialInformationEntity.setPriceEndData(((Date) returnTable.getValue("PRDAT")));
            essentialInformationEntity.setStandardNum(new BigDecimal(returnTable.getValue("NORBM").toString()));
            essentialInformationEntity.setCycle(new BigDecimal(returnTable.getValue("APLFZ").toString()));
            essentialInformationEntity.setPriceUnit(returnTable.getValue("PEINH").toString());
            //采购信息记录分类
            essentialInformationEntity.setClassify((String) returnTable.getValue("ESOKZ"));
            essentialInformationEntity.setInfoStatus(1);
            if (returnInfo.getValue("TYPE").equals("S")) {
                try {
                    throw new Exception("SAP同步错误" + returnInfo.getValue("MESSAGE"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                //查询重复数据
                EssentialInformationEntity inFo = this.essentialInformationServiceimpl.findByNo(essentialInformationEntity.getSrmCode(), essentialInformationEntity.getWerks(), essentialInformationEntity.getMaterialCode(), essentialInformationEntity.getClassify());
                if (inFo == null) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    essentialInformationEntity.setPkId(uuid);
                    this.essentialInformationServiceimpl.insertEssntiaInfo(essentialInformationEntity);
                    InforecordLogEntity logEntity = new InforecordLogEntity();
                    DozerUtil.map(essentialInformationEntity, logEntity);
                    logEntity.setSapOrSrm("sap");
                    logEntity.setCreatetime(new Date());
                    this.inforecordLogService.insert(logEntity);
                }
            }

        }
    }

}
